package com.frxuexi.document

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.document.entity.DocEntity
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RequestType
import com.frxuexi.lib_base.RouterPath
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@Composable
fun DocPage(navHostController: NavHostController, vm: DocViewModel = hiltViewModel()) {
    var list = remember {
        SnapshotStateList<DocEntity>()
    }
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect {
                when (it) {
                    is IUiState.onSuccess<*> -> {
                        if (it.type == RequestType.DEFAULT) {
                            list.clear()
                            Log.i("===", it.data.toString())
                            list.addAll(it.data as Collection<DocEntity>)
                        } else if (it.type == RequestType.DELETE) {
                            ToastUtils.showLong("删除成功")
                            //发送意图
                            vm.sendIntent(DocIntent.getDocs(mapOf("userId" to 1, "page" to 1)))
                        }
                    }

                    is IUiState.onError -> {
                        Log.i("===", it.msg)
                    }

                    else -> {}
                }
            }
        }

        //发送意图
        vm.sendIntent(DocIntent.getDocs(mapOf("userId" to 1, "page" to 1)))
    })

    Scaffold(floatingActionButton = {
        Image(
            painter = painterResource(id = R.drawable.icon159),
            contentDescription = "",
            Modifier.clickable {
                navHostController.navigate(RouterPath.LOCALDOC_PAGE)
            })
    }) {
        Column(Modifier.padding(it)) {
            MyTitleBar(navHostController = navHostController, title = "文件")
            LazyColumn(
                Modifier.padding(20.dp, 10.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp),
                content = {
                    items(list) {
                        DocItem(entity = it, vm)
                    }
                })
        }
    }


}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun DocItem(entity: DocEntity, vm: DocViewModel) {
    var isShow by remember {
        mutableStateOf(false)
    }
    Row(modifier = Modifier.combinedClickable(onClick = {
        ToastUtils.showLong("点击了")
    }, onDoubleClick = {
        ToastUtils.showLong("双击了")
    }, onLongClick = {
        isShow = true
    })) {
        AsyncImage(
            model = entity.filePath, contentScale = ContentScale.Crop, contentDescription = "",
            modifier = Modifier
                .size(60.dp)
                .clip(RoundedCornerShape(20.dp))
        )
        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(60.dp), verticalArrangement = Arrangement.SpaceBetween) {
            Text(text = entity.fileName)
            val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(entity.fileTime)
            Text(text = time)
        }
    }

    if (isShow) {
        AlertDialog(onDismissRequest = { isShow = false },
            confirmButton = {
                Text(text = "确认", Modifier.clickable {
                    vm.sendIntent(DocIntent.deleteDoc(entity.fileId))
                    isShow = false
                })
            }, dismissButton = {
                Text(text = "取消", Modifier.clickable {
                    isShow = false
                })
            }, text = {
                Text(text = "是否删除${entity.fileName}吗？")
            }, title = {
                Text(text = "删除提醒")
            })
    }
}